package com.doublefish.doublefishrobot.ui.setting.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.doublefish.doublefishrobot.R;

import java.util.List;

public class LocalPDFAdapter extends BaseQuickAdapter<LocalPDFAdapter.LocalPdfBean, LocalPDFAdapter.PDFViewHolder> {

    private Context mContext;
    private OnGetBitmapCallback onGetBitmapCallback;

    private int scrolledMaxPosition = 0;

    public LocalPDFAdapter(Context context, List<LocalPdfBean> data) {
        super(R.layout.item_pdf_view, data);
        mContext = context;
    }

    @Override
    protected void convert(LocalPDFAdapter.PDFViewHolder helper, LocalPdfBean item) {
        if (item.getIndex() > scrolledMaxPosition) {
            scrolledMaxPosition = item.getIndex();
        }
        if (item.getBitmap() == null) {
            helper.imageView.setImageBitmap(null);
            if (onGetBitmapCallback != null) {
               Bitmap bm = onGetBitmapCallback.onGetBitmap(item);
                helper.imageView.setImageBitmap(bm);
            }
        } else {
            helper.imageView.setImageBitmap(item.getBitmap());
        }
    }

    public void setOnGetBitmapCallback(OnGetBitmapCallback onGetBitmapCallback) {
        this.onGetBitmapCallback = onGetBitmapCallback;
    }

    public int getScrolledMaxPosition() {
        return scrolledMaxPosition;
    }

    class PDFViewHolder extends BaseViewHolder {

        ImageView imageView;

        public PDFViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.imageview);
        }
    }

    public static class LocalPdfBean {

        private int index;
        private Bitmap bitmap;

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public Bitmap getBitmap() {
            return bitmap;
        }

        public void setBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
        }
    }

    public interface OnGetBitmapCallback {
        Bitmap onGetBitmap(LocalPdfBean pdfBean);
    }
}
